package com.core.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.core.dao.GenericDao;
import com.core.entity.DomainObject;
import com.core.service.GenericService;

public abstract class GenericServiceImpl<E extends DomainObject, PK extends Serializable>
		implements GenericService<E, PK> {

	@Override
	@Transactional
	public PK save(E entity) throws Exception {
		try {
			return getDao().save(entity);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Cannot save new data! Please contact your Administrator");
		}

	}

	@Override
	@Transactional
	public void update(E transientObject) throws Exception {
		try {
			getDao().update(transientObject);
		} catch (Exception e) {
			throw new Exception("Cannot update data! Please contact your Administrator");
		}
	}

	@Override
	@Transactional
	public void saveOrUpdate(E transientObject) throws Exception {
		try {
			getDao().saveOrUpdate(transientObject);
		} catch (Exception e) {
			throw new Exception("Cannot save data! Please contact your Administrator");
		}
	}

	@Override
	@Transactional
	public void delete(E persistentObject) throws Exception {
		try {
			getDao().delete(persistentObject);
		} catch (Exception e) {
			throw new Exception("Cannot delete data! Please contact your Administrator");
		}
	}

	@Override
	@Transactional(readOnly=true)
	public E find(PK id) {
		return getDao().find(id);
	}

	@Override
	@Transactional(readOnly=true)
	public List<E> findAll() {
		return getDao().findAll();
	}

	@Override
	@Transactional(readOnly=true)
	public List<E> findAllByProperty(String propertyName, Object value) {
		return getDao().findAllByProperty(propertyName, value);
	}

}
